package com.mxpio.erp.equipment.provider;

import com.mxpioframework.jpa.query.Criterion;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.jpa.query.SimpleCriterion;
import com.mxpioframework.security.access.datascope.provider.DataScapeContext;
import com.mxpioframework.security.access.datascope.provider.DataScapeProvider;
import com.mxpioframework.security.util.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author : 吴盼盼
 * @Datetime : 2022/11/3 15:44
 * @Desc :
 * @Modor :  Modifytime:
 * @modDesc : distributePersonId 为当前登录人(维修单任务分配人)
 */
@Component
public class DistributePersonIdProvider implements DataScapeProvider {
    @Override
    @Transactional(readOnly = true)
    public List<Criterion> provide(DataScapeContext context) {
        String loginUsername = SecurityUtils.getLoginUsername();
        List<Criterion> appendCriterions = new ArrayList<>();
        if (StringUtils.isBlank(loginUsername)){
            appendCriterions.add(new SimpleCriterion("createTime", Operator.EQ,new Date(0)));//如果未匹配到过滤条件，则增加一个不能正常匹配到数据的条件
            return appendCriterions;
        }
        appendCriterions.add(new SimpleCriterion("distributePersonId", Operator.EQ, loginUsername));
        return appendCriterions;
    }
}
